package com.fzu.orderservice.controller;


import com.fzu.commonutil.entity.User;
import com.fzu.commonutil.params.DataResult;
import com.fzu.commonutil.util.CodeEnum;
import com.fzu.commonutil.util.ErrorEnum;
import com.fzu.commonutil.util.JwtUtil;
import com.fzu.commonutil.util.R;
import com.fzu.orderservice.params.ConfirmParam;
import com.fzu.orderservice.params.EndParam;
import com.fzu.orderservice.params.DemandToOrderParam;
import com.fzu.orderservice.params.HousekeeperToOrderParam;
import com.fzu.orderservice.service.OrderFormService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.fzu.commonutil.entity.OrderForm;

import javax.validation.Valid;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/order-form")
@RequiredArgsConstructor
public class OrderFormController {
    private final OrderFormService orderFormService;

    /**
     * 订单分页列表
     * @param orderForm
     * @param pageNum
     * @param pageSize
     * @param token
     * @return
     */
    @GetMapping
    public R page(OrderForm orderForm,
                  @RequestParam(required = false, defaultValue = "10") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "1") Integer pageSize,
                  @RequestHeader("token") String token) {
        return R.ok().data(orderFormService.page(orderForm, JwtUtil.getUserFromRedis(token), pageNum, pageSize));
    }

    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        return R.ok().data(orderFormService.detail(id));
    }


    /**
     * 从需求匹配列表创建订单
     * @param param
     * @param token
     * @return
     */
    @PostMapping("/demand-to-order")
    public R demandToOrder(@RequestBody @Valid DemandToOrderParam param,
                         @RequestHeader("token") String token) {
        orderFormService.demandToOrder(param, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }


    /**
     * 从家政员大厅创建订单
     * @param param
     * @param token
     * @return
     */
    @PostMapping("/housekeeper-to-order")
    public R housekeeperToOrder(@RequestBody @Valid HousekeeperToOrderParam param,
                         @RequestHeader("token") String token) {
        orderFormService.housekeeperToOrder(param, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public R waitConfirmToCancel(@PathVariable("id") Integer id) {
        orderFormService.cancel(id);
        return R.ok();
    }


    /**
     * 公司接单
     * @param id
     * @param token
     * @return
     */
    @PutMapping("/accept/{id}")
    public R accept(@PathVariable("id") Integer id,
                                 @RequestHeader("token") String token) {
        orderFormService.accept(id, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }


    /**
     * 用户支付
     * @param id
     * @param token
     * @return
     */
    @PutMapping("/pay/{id}")
    public R pay(@PathVariable("id") Integer id,
                 @RequestHeader("token") String token) {
        orderFormService.pay(id, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }


    /**
     * 用户确认开始
     * @param id
     * @param token
     * @return
     */
    @PutMapping("/start/{id}")
    public R start(@PathVariable("id") Integer id,
                 @RequestHeader("token") String token) {
        orderFormService.start(id, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }


    /**
     * 用户确认结束并评价
     * @param param
     * @param token
     * @return
     */
    @PutMapping("/end")
    public R end(@RequestBody @Valid EndParam param,
                 @RequestHeader("token") String token) {
        orderFormService.end(param, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }

    /**
     * 公司确认
     * @param param
     * @param token
     * @return
     */
    @PutMapping("/confirm")
    public R confirm(@RequestBody ConfirmParam param,
                    @RequestHeader("token") String token) {
        orderFormService.confirm(param, JwtUtil.getUserFromRedis(token));
        return R.ok();
    }

    /**
     * 获取订单统计数据
     * @return
     */
    @GetMapping("/data")
    public R data(@RequestHeader("token") String token) {
        User user = JwtUtil.getUserFromRedis(token);
        if (!CodeEnum.COMPANY.getCode().equals(user.getRole())) {
            user.setCompanyId(null);
        }
        Integer companyId = user.getCompanyId();
        Integer role = user.getRole();
        CompletableFuture<DataResult> future1 = orderFormService.asyncTask1(companyId);
        CompletableFuture<DataResult> future2 = orderFormService.asyncTask2(companyId, role);
        DataResult dataResult = new DataResult();
        try {
            DataResult result2 = future2.get();
            DataResult result1 = future1.get();
            dataResult.setTypeData(result2.getTypeData());
            dataResult.setTypeLabel(result2.getTypeLabel());
            dataResult.setOrderCount(result2.getOrderCount());
            dataResult.setOrderData(result1.getOrderData());
            dataResult.setOrderLabel(result1.getOrderLabel());
        } catch (Exception e) {
            return R.error(ErrorEnum.BUSY);
        }
        return R.ok().data(dataResult);
    }


}

